﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.SafeManage;
using LJX.SmartPark.EntityFramework;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.SafeManage
{
    public class PatrolpathService : IPatrolpathService
    {
        private readonly IRepository<Patrolpath> _studentService;

        private readonly StudentServerContext _context;

        public PatrolpathService(IRepository<Patrolpath> studentService, StudentServerContext context)
        {
            _studentService = studentService;
            _context = context;
        }

        public async Task<Response<bool>> DeleteByIdAsync(Patrolpath input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<Patrolpath>());
            List<Patroldetail> list = _context.Patroldetail.Where(x=>x.LineId == input.Id).ToList();
            _context.RemoveRange(list);
            _context.SaveChanges();
            return Response<bool>.Ok(result > 0);
        }

        public object GetPatrolpath(PatrolpathForm form)
        {
            var students = _studentService.GetListQuery().ToList();
            var tail = _context.Patroldetail.ToList();
            var collocate = _context.PatrolCollocate.ToList();
            var data = students.Select(a => new
            {
                a.Id,
                a.Name,
                a.Remark,
                a.Sum,
                PatroName = tail.Where(x => x.LineId == a.Id).Join(collocate, b => b.HouseId, c => c.Id, (b, c) => new
                {
                    name =c.Name
                })
            });

            if (!string.IsNullOrEmpty(form.name))
            {
                data = data.Where(x => x.Name.Contains(form.name)).ToList();
            }
            var count = students.Count();
            data = data.Skip((form.pageIndex - 1) * form.pageSize).Take(form.pageSize).ToList();
            return new { data, count };
        }

        public async Task<Response<bool>> PostAddPatrolpath(Patrolpath input)
        {
            input.Sum = 0;
            var result = _studentService.InsertAsyncById(input.MapTo<Patrolpath>());

            return Response<bool>.Ok(result.Result > 0);
        }

        public async Task<Response<bool>> PutUpdatePatrolpath(Patrolpath input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<Patrolpath>());

            return Response<bool>.Ok(result.Result > 0);
        }

        public class aaa
        {
            public int Id { get; set; }

            public string Name { get; set; }

            public string Remark { get; set; }

            public int Sum { get; set; }

            public List<PatroName> PatroName { get; set; }
        }

        public class PatroName
        {
            public string name { get; set; }
        }
    }
}
